<template>
	<view style="background: #fff">
		<u-navbar :bgColor="bgColor" leftIconColor="#fff" title="我的积分" :titleStyle="{ color: '#ffffff' }"></u-navbar>
		<view class="content">
			<view class="t-wrap">
				<view class="t-jf">
					<view class="t-jf-points">
						<view class="t-jf-title">我的积分</view>
						<view class="t-jf-info">
							<view class="t-jf-num">{{ signdata.pointTotal }}</view>
							<view class="t-jf-detail" hover-class="t-hc" @click="toDetail">
								<text>积分明细</text>
								<image src="/static/icon-right.png"></image>
							</view>
						</view>
					</view>
					<!-- <view class="">
						<u--image :showLoading="true" :src="tz" width="200rpx" height="65rpx"></u--image>
					</view> -->
					<!-- <view class="t-jf-mall" hover-class="t-hc" @click="toMall">
					积分商城
				</view> -->
				</view>
				<view class="t-bg-wrap">
					<view class="t-bg"></view>
				</view>
			</view>
			<view class="t-sign-con">
				<view class="wrap">
					<view class="t-sign-top">
						<view class="lx">已连续签到<text>{{ signdata.continuousDay }}</text>天</view>
						<view class="gz" hover-class="t-hc" @click="openRule">积分规则<image src="/static/icon-ask.png">
							</image>
						</view>
					</view>

					<view style="background-color: #fff; padding: 20rpx; padding-bottom: 0">
						<u-calendar-index ref="calendar" :islogin="islogin" @setItem="setItem"></u-calendar-index>
					</view>
					<view v-if="!dqitem.dot && dqitem.status < 0" style="padding: 20rpx 0">
						<u-notice-bar :step="true" direction="column" :text="bqtext"></u-notice-bar>
					</view>
					<view @click="toSign" :class="[
              't-sign-btn',
              { 't-signed': !(!dqitem.dot && dqitem.status <= 0) },
            ]">
						<template v-if="dqitem.dot">今日已签到</template>
						<template v-else-if="dqitem.status > 0">暂不能签到</template>
						<template v-else-if="dqitem.status == 0">立即签到</template>
						<template v-else-if="dqitem.status < 0">立即补签</template>
					</view>
				</view>
			</view>
			<view class="t-task">
				<view class="t-title">做任务 赚积分</view>
				<view class="t-task-item">
					<view class="left">
						<view class="taskname"> 分享商品<text>+15积分</text> </view>
						<view class="taskdesc"> 分享任意商品即可完成任务 3次/日 </view>
					</view>
					<view class="right" hover-class="t-hc" @click="toShare">去分享({{ 3 - share }}/3)</view>
				</view>
				<view class="t-task-item">
					<view class="left">
						<view class="taskname"> 浏览商品<text>+10积分</text> </view>
						<view class="taskdesc"> 浏览任意商品即可完成任务 2次/日 </view>
					</view>
					<view class="right" hover-class="t-hc" @click="toBrowse">去浏览({{ 2 - browse }}/2)</view>
				</view>
				<!-- <view class="t-task-item">
					<view class="left">
						<view class="taskname">
							浏览商品页20秒<text>+50积分</text>
						</view>
						<view class="taskdesc">
							3次/日
						</view>
					</view>
					<view class="right" hover-class="t-hc">去浏览</view>
				</view>
				<view class="t-task-item">
					<view class="left">
						<view class="taskname">
							分享至朋友圈<text>+15积分</text>
						</view>
						<view class="taskdesc">
							2次/日
						</view>
					</view>
					<view class="right" hover-class="t-hc">去分享</view>
				</view> -->
			</view>
			<!-- 弹出层-签到领积分 -->
			<kevy-mask :show="showSignBox" @click="closeSign" :maskClose="true" :z-index="10">
				<!-- <view v-if="showSignBox" class="t-overlay" @mousemove.stop @click="closeSign"> -->
				<view class="t-o-wrap">
					<image class="t-sign-bg" src="/static/sign.png"></image>
					<view class="t-g-jf">+{{ showSignData.point }}积分</view>
					<view class="t-g-desc">已连续签到{{ showSignData.day }}天，继续签到有惊喜哦~</view>
					<view class="close-btn" @click="closeSign">确认</view>
				</view>
			</kevy-mask>
			<!-- 弹出层-签到领积分 -->
			<kevy-mask :show="showSignBox2" @click="closeSign" :maskClose="true" :z-index="10">
				<!-- <view v-if="showSignBox" class="t-overlay" @mousemove.stop @click="closeSign"> -->
				<view class="t-o-wrap">
					<image class="t-sign-bg" src="/static/sign2.png"></image>
					<view class="t-g-jf">- {{showSignData}} 积分</view>
					<view class="t-g-desc"></view>
					<view class="close-btn" @click="closeSign">确认</view>
				</view>
			</kevy-mask>
			<!-- showSignBox2 -->
			<!-- </view> -->
			<!-- 弹出层-积分规则 -->
			<kevy-mask :show="showRuleBox" @click="closeRule" :maskClose="true" :z-index="10">
				<view class="t-o-wrap">
					<view class="t-rule">
						<view class="title"> 积分规则说明 </view>
						<view class="desc">
							{{ rule }}
						</view>
					</view>
					<image @click="closeRule" class="t-close" src="../../static/close.png"></image>
				</view>
			</kevy-mask>
		</view>
	</view>
</template>

<script>
	import {
		getSignInRecordSummary,
		createSignInRecord,
		pointsRemainTimes,
		supplementSign,
	} from "@/api/points.js";

	import kevyMask from "@/components/kevy-mask/kevy-mask.vue";
	import {
		getAccessToken
	} from "@/utils/auth";
	export default {
		components: {
			kevyMask,
		},
		data() {
			return {
				bqtext: ["补签需要消耗 20 积分"],
				signdata: {},
				bgColor: "#39C03E",
				//今日是否签到
				isSigned: false,
				//是否显示签到弹框
				showSignBox: false,
				showSignBox2: false,
				//是否显示积分规则弹框
				showRuleBox: false,
				showSignData: {},
				//积分规则说明
				rule: "",
				// 浏览次数
				browse: 0,
				// 分享次数
				share: 0,
				islogin: false,
				// 当前选中日期
				dqitem: {},
			};
		},
		onLoad() {
			// this.init()
		},
		onShow() {
			this.init();
		},

		methods: {
			async init() {
				let islogin = getAccessToken() ? true : false;
				this.islogin = islogin;
				if (!islogin) {
					return;
				}
				let res = await getSignInRecordSummary();
				console.log(res, "res");
				this.signdata = res.data;
				this.isSigned = res.data.todaySignIn || false;
				let browseShare = await pointsRemainTimes();
				browseShare = browseShare.data;

				this.browse = Number(browseShare.browse) || 0;
				this.share = Number(browseShare.share) || 0;

				this.$refs.calendar.initlist();
			},
			//签到点击
			async toSign() {
				let islogin = getAccessToken() ? true : false;
				if (!islogin) {
					uni.navigateTo({
						url: "/pages/login/login",
					});
					return;
				}
				if (this.dqitem.dot || this.dqitem.status > 0) {
					return;
				}
				if (this.dqitem.status == 0) {
					let res = await createSignInRecord();
					this.showSignData = res.data;
					//这里写签到逻辑，这里模拟成功后操作
					this.showSignBox = true;
					this.init();
				} else {
					// console.log(this.dqitem.date);
					// return
					let res = await supplementSign({
						supplementDate: this.$u.timefn.gettime(0, this.dqitem.date),
					});
					this.showSignData = res.data;
					//这里写签到逻辑，这里模拟成功后操作
					this.showSignBox2 = true;
					// this.$u.toast("补签成功，消耗20积分")
					this.init();
				}
				//
			},
			//关闭签到弹框--签到成功后点击
			closeSign() {
				this.showSignBox = false;
				this.showSignBox2 = false;
				// this.isSigned = true;
			},
			//打开签到弹框
			openRule() {
				uni.navigateTo({
					url: "/pages/parse/parse?type=SIGN_IN_RULES",
				});
				// this.showRuleBox = true;
			},
			//关闭签到弹框
			closeRule() {
				this.showRuleBox = false;
			},
			//跳转积分商城
			toMall() {
				uni.navigateTo({
					url: "/pages/mall/mall",
				});
			},
			//跳转积分明细
			toDetail() {
				let islogin = getAccessToken() ? true : false;
				if (!islogin) {
					uni.navigateTo({
						url: "/pages/login/login",
					});
					return;
				}
				uni.navigateTo({
					url: "/pages/points/detail/detail",
				});
			},
			//跳转分享页面
			toShare() {
				let islogin = getAccessToken() ? true : false;
				if (!islogin) {
					uni.navigateTo({
						url: "/pages/login/login",
					});
					return;
				}
				if (this.share == 3) {
					this.$u.toast("今日分享次数已达上限,请明日再分享");
					return;
				}
				uni.navigateTo({
					url: "/pages/shop/goods/list/list?type=share",
				});
			},
			toBrowse() {
				let islogin = getAccessToken() ? true : false;
				if (!islogin) {
					uni.navigateTo({
						url: "/pages/login/login",
					});
					return;
				}
				if (this.browse == 2) {
					this.$u.toast("今日浏览次数已达上限,请明日再浏览");
					return;
				}
				uni.navigateTo({
					url: "/pages/shop/goods/list/list?type=browse",
				});
			},
			setItem(item) {
				this.dqitem = item;
				console.log(item, 123);
			},
		},
	};
</script>

<style scoped lang="scss">
	.content {
		box-sizing: border-box;
		padding-bottom: 20rpx;
		background: #fff;
		min-width: 100vw;
		min-height: 100vh;
	}

	.t-hc {
		opacity: 0.7;
	}

	.t-wrap {
		height: 280rpx;
		width: 100%;
		box-sizing: border-box;

		.t-jf {
			width: 100%;
			height: 100%;
			box-sizing: border-box;
			padding-bottom: 40rpx;
			background: linear-gradient(to bottom, #39c03e, #39c03e);
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.t-jf-points {
				padding-left: 70rpx;

				.t-jf-title {
					font-size: 32rpx;
					color: #fff;
				}

				.t-jf-info {
					margin-top: 20rpx;
					display: flex;
					flex-direction: row;
					justify-content: flex-start;
					align-items: flex-end;

					.t-jf-num {
						font-size: 56rpx;
						font-weight: bold;
						color: #fff;
						line-height: 56rpx;
					}

					.t-jf-detail {
						display: flex;
						flex-direction: row;
						justify-content: flex-start;
						align-items: center;

						text {
							font-size: 24rpx;
							color: #fff;
							margin-left: 20rpx;
						}

						image {
							width: 18rpx;
							height: 18rpx;
							margin-left: 6rpx;
						}
					}
				}
			}

			.t-jf-mall {
				margin-right: 70rpx;
				padding: 15rpx 30rpx;
				background: #fff;
				border-radius: 50rpx;
				color: #1abb7c;
			}
		}

		.t-bg-wrap {
			position: relative;
			align-self: flex-start;
			top: 54rpx;

			.t-bg {
				--width: 440vw;
				position: absolute;
				height: var(--width);
				width: var(--width);
				left: calc(var(--width) * -1 / 2 + 50vw - 40rpx);
				top: calc(var(--width) * -1 + 1vw);
				border-radius: 50%;
				z-index: -1;
				background: linear-gradient(to bottom, #39c03e, #39c03e);
			}
		}
	}

	.t-sign-con {
		width: 100%;
		box-sizing: border-box;
		position: relative;
		padding: 0rpx 30rpx;

		.wrap {
			width: 100%;
			// height: 360rpx;
			border-radius: 10rpx;
			background-color: #fff;
			position: relative;
			top: -50rpx;
			box-sizing: border-box;
			box-shadow: 0 30rpx 50px rgba(0, 0, 0, 0.4);
			padding-bottom: 32rpx;

			.t-sign-top {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				box-sizing: border-box;
				width: 100%;
				padding: 28rpx 20rpx;

				.lx {
					font-size: 32rpx;
					color: #333;

					text {
						color: #18ba7a;
						margin: 0rpx 3rpx;
					}
				}

				.gz {
					display: flex;
					flex-direction: row;
					justify-content: flex-start;
					align-items: center;
					font-size: 28rpx;
					color: #999;

					image {
						width: 28rpx;
						height: 28rpx;
						margin-left: 8rpx;
					}
				}
			}

			.t-sign-body {
				height: 124rpx;
				width: 100%;
				box-sizing: border-box;
				padding: 0rpx 20rpx;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;

				.t-sign-item {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					height: 100%;
					box-sizing: border-box;
					padding: 0rpx 12rpx;
					border-radius: 10rpx;

					&.signed {
						background: #27ba76;

						text {
							color: #fff !important;
						}
					}

					.t-img-con {
						width: 56rpx;
						height: 56rpx;
						border-radius: 50%;
						background: #39c03e;
						display: flex;
						flex-direction: row;
						justify-content: center;
						align-items: center;

						.t-img {
							width: 40rpx;
							height: 40rpx;
							border-radius: 50%;
							background: #39c03e;
							display: flex;
							flex-direction: row;
							justify-content: center;
							align-items: center;

							image {
								width: 24rpx;
								height: 24rpx;
							}
						}
					}

					text {
						font-size: 20rpx;
						color: #777;
						margin-top: 12rpx;
					}
				}
			}

			.t-sign-btn {
				width: 514rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				border-radius: 50rpx;
				background-color: #39c03e;
				font-size: 32rpx;
				color: #fff;
				margin: 0rpx auto 0rpx;
				position: relative;
				// top: 50%;
				// transform: translateY(-50%);

				&.t-signed {
					background-color: #b8b8b8 !important;
				}
			}
		}
	}

	.t-task {
		box-sizing: border-box;
		background: #fff;
		padding: 0rpx 30rpx;
		margin-top: 20rpx;

		.t-title {
			font-size: 32rpx;
			color: #000;
			text-align: center;
		}

		.t-task-item {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			height: 130rpx;
			box-sizing: border-box;
			border-bottom: 1rpx solid #cdcdcd;

			.left {
				.taskname {
					max-width: 550rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					font-size: 28rpx;
					color: #333;

					text {
						color: #39c03e;
						margin-left: 10rpx;
					}
				}

				.taskdesc {
					max-width: 510rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					font-size: 26rpx;
					color: #999;
					margin-top: 14rpx;
				}
			}

			.right {
				padding: 16rpx 34rpx;
				border-radius: 50rpx;
				background-color: #39c03e;
				text-align: center;
				font-size: 28rpx;
				color: #fff;

				&.finish {
					background-color: #b8b8b8 !important;
				}
			}
		}
	}

	.t-o-wrap {
		width: 100%;
		position: relative;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;

		.t-sign-bg {
			width: 550rpx;
			border-bottom-left-radius: 20rpx;
			border-bottom-right-radius: 20rpx;
		}

		.t-g-jf {
			position: relative;
			top: -220rpx;
			font-size: 36rpx;
			color: #39c03e;
		}

		.t-g-desc {
			position: relative;
			top: -200rpx;
			font-size: 28rpx;
			color: #999;
			height: 28rpx;
		}

		.close-btn {
			position: relative;
			top: -173rpx;
			width: 300rpx;
			height: 60rpx;
			border-radius: 50rpx;
			background: #39c03e;
			text-align: center;
			line-height: 60rpx;
			font-size: 28rpx;
			color: #fff;
		}

		.t-rule {
			width: 690rpx;
			background: #fff;
			border-radius: 20rpx;
			padding: 30rpx;
			box-sizing: border-box;

			.title {
				text-align: center;
				color: #333;
				font-size: 32rpx;
				margin-top: 10rpx;
			}

			.desc {
				text-align: justify;
				margin-top: 20rpx;
				white-space: pre-line;
				font-size: 32rpx;
				color: #666;
				line-height: 50rpx;
			}
		}

		.t-close {
			width: 60rpx;
			height: 60rpx;
			margin-top: 40rpx;
			padding: 10rpx;
		}
	}
</style>